package com.join.template.html.node;

import cn.hutool.core.util.StrUtil;
import com.join.template.core.constant.Constant;
import com.join.template.core.constant.MarkedWords;
import com.join.template.core.element.Element;
import com.join.template.core.element.verify.NodeVerify;
import com.join.template.core.example.AbstractExample;
import com.join.template.core.grammar.handle.Grammar;
import com.join.template.core.grammar.info.GrammarInfo;
import com.join.template.core.util.TemplateException;
import lombok.Getter;


import java.util.HashMap;
import java.util.Map;

/**
 * @author CAOYOU
 * @Title: ListNode
 * @date 2019/10/1815:52
 */
@Getter
public class ListNodeExample extends AbstractExample implements NodeVerify, Element {
    //值属性
    private String var;
    //别名属性
    private String separator;
    //单项别名属性
    private String item;
    //语句开始属性
    private String open;
    //索引属性
    private String index;
    //语句结束属性
    private String close;


    @Override
    public void readAttributes() {
        this.var = this.getAttribute(expression.getAttVar(), "");
        this.item = this.getAttribute(expression.getAttItem(), "");
        this.index = this.getAttribute(expression.getAttIndex(), "");
        this.separator = MarkedWords.removeHint(this.getAttribute(expression.getAttSseparator()), MarkedWords.AttrSeparator);
        this.open = MarkedWords.removeHint(this.getAttribute(expression.getAttOpen()), MarkedWords.AttrStatementOpener);
        this.close = MarkedWords.removeHint(this.getAttribute(expression.getAttClose()), MarkedWords.AttrStatementTerminator);
    }


    @Override
    public void verify() {
        if (!attributes.containsKey(expression.getAttVar())) {
            throw new TemplateException("请求设置循环条件-数据来源别名（" + expression.getAttVar() + "）：" + original);
        }
        if (!attributes.containsKey(expression.getAttItem())) {
            throw new TemplateException("请求设置循环条件-单项定义别名（" + expression.getAttItem() + "）：" + original);
        }
    }

    @Override
    public String getExample() {
        Grammar expressionElse = joinFactory.getGrammar(Constant.EXPR_ELSE);

        Map<String, String> attr = new HashMap<>();
        attr.put(expression.getAttVar(), MarkedWords.getAttrVarcharName());
        attr.put(expression.getAttItem(), MarkedWords.getAttrItemName());
        attr.put(expression.getAttOpen(), MarkedWords.getAttrStatementOpener());
        attr.put(expression.getAttClose(), MarkedWords.getAttrStatementTerminator());
        attr.put(expression.getAttSseparator(), MarkedWords.getAttrSeparator());
        String attribute = joinFactory.createExprAttr().genAttribute(attr);

        StringBuilder grammar = new StringBuilder();
        grammar.append(expression.getExprFirstBegin()).append(this.grammar.getTag()).append(" ");
        grammar.append(attribute);
        grammar.append(expression.getExprEndSupport());
        grammar.append(MarkedWords.getHintInputGeneratedContent());
        grammar.append(expression.getExprFirstBegin()).append(expressionElse.getTag()).append(expression.getExprEndSupport());
        grammar.append(MarkedWords.getHintInputGeneratedDefaultContent());
        grammar.append(expression.getExprLastBegin()).append(this.grammar.getTag()).append(expression.getExprEndSupport());
        return grammar.toString();
    }

    @Override
    public String genExample(GrammarInfo grammarInfo) {
        Grammar expressionElse = joinFactory.getGrammar(Constant.EXPR_ELSE);
        StringBuilder var = new StringBuilder();
        if (grammarInfo != null && StrUtil.isNotBlank(grammarInfo.getParentName())) {
            var.append(grammarInfo.getParentName());
            var.append(".");
        }
        var.append(grammarInfo.getName());

        Map<String, String> attr = new HashMap<>();
        attr.put(expression.getAttVar(), var.toString());
        attr.put(expression.getAttItem(), grammarInfo.getAlias());
        attr.put(expression.getAttOpen(), MarkedWords.getAttrStatementOpener());
        attr.put(expression.getAttClose(), MarkedWords.getAttrStatementTerminator());
        attr.put(expression.getAttSseparator(), MarkedWords.getAttrSeparator());
        String attribute = joinFactory.createExprAttr().genAttribute(attr);

        StringBuilder grammar = new StringBuilder();
        grammar.append(expression.getExprFirstBegin()).append(this.grammar.getTag()).append(" ");
        grammar.append(attribute);
        grammar.append(expression.getExprEndSupport());
        grammar.append(MarkedWords.getHintInputGeneratedContent());
        grammar.append(expression.getExprFirstBegin()).append(expressionElse.getTag()).append(expression.getExprEndSupport());
        grammar.append(MarkedWords.getHintInputGeneratedDefaultContent());
        grammar.append(expression.getExprLastBegin()).append(this.grammar.getTag()).append(expression.getExprEndSupport());
        return grammar.toString();
    }


}
